package com.syyo.gulimall.order.config.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.syyo.gulimall.order.domain.vo.PayVo;
import lombok.Data;

import org.springframework.stereotype.Component;

@Component
@Data
public class AlipayTemplate {

    // 买家账号  hemqst7845@sandbox.com

    //在支付宝创建的应用的id
    private   String app_id = "2016091800541828";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCofrkHMti6AtXk1K5F0iAh2yfCr1NRAdymOzmquzb+moVkGkhLoItMP3o29sg8PNYyNs5Is+BLw1csJ8qrO6nVdBZ6q1vS7ycv/l4gfdTg5xp+2x1nvrzDJHwYUDfZzvERZ8qCU7zEhQU7DtAMLnpPserdi2fx/QQ/UkbK1H50NbTxlBiEWH6Yt+wYfE+w/ofVf/6JjEtn++dCRPSKq83zNYs0V/vUpfzd1CAiLUWVS70vZfCRPPieSyE7xRZ3OZcTMQlpj3XbGAJ6pK3gkBkNUoOUPk4IDtVklrR8x9zyimGFh2QgO8DgQVUXwpeB6oezpZYiLUrfGZL4XibtX7cFAgMBAAECggEAVGfWzJJeGyBcgdtAyd0vF4avbklh1Uy3edZlVu+d5wEFHVZFLQ1penK6CFqrh/F544Gs6KKjE5DxVD9CuXCT7UfQEvdQcmQ3H0WvZVtjjp52mWGuY1AbQy49s/hLYiLWhD+YJJ1KDLAOJL2Q3ulRgCbCXuT81y5GOGpA6E52YkPMbon5YPIDjI++aBaLXTYLGoikqaTY//7IBCgl0o++18j49wAqCyJDbo3/UeuhwNQiucaTvteoK6JadQlkeqoV6cL4JA0mxpQguFF7et+ZQkLeQi1MEzgAmJrGdKyhXaPCaRpZGykBKeFQivOcrfVU2w5JSCrEGv8+JsC+rSntgQKBgQDhcTwvQdQcmKHRFJ2I3LthR+M2veCKCv0Bzxew+GZY5+6qX1YpntjqDjbPqD+Ln6tAWsha7lACYyNH6io+26Zp3Z+ADjUY9KvBUEAWaEe1b/VEKNQjO3dRHjQYSkh+WSjHwTxLc9aVUiVsr01Ri2J7pjGuQNEYWkAtl0uHg29JkQKBgQC/VW/h0EjtzzcxDYgx3OgmALmHekAsL5FWfotuAE0GkbofgrNuF7MKShsfya0x3p/aB6ZCZGhwZWZW3gMiD2Xx9pJJMGSWSymm1EHorD0Q1b2vqvRHZbUyROSXHg8Y5pnyX6ZUEJxoJvzmlM97AbKdzhRRgUqiexdtCV9PaN+8NQKBgQDaREKr0kUlxpTsk9QN/P+iKPKQPnfVztu0U6exXF3i9/K3mwQV2chTm51voYqciONJtFtExnRKS2eOKeXq48iTuNHDLn40AjcEO8lAqIe833+MFSeGUP2tWNvBJLJZOcNk2u6gcq+v9E9tka//Ij8gTgDNn82RkVePjGrA8fzeMQKBgHJVIYg0qH/CxV5AkdQKB4c+A5+bQ+GjgxvGfpb7Awwb5LaTqZjt/IT3h4HVR/N27tqgm48s5OPVE/RwsjqWLPsLtpqZmmHdBES2rbPVyJ/YuYLRiytm8YHafSZ4RdlBY1jcI040puHt+k3nOpxshUmbLbUOp4S0PWlgol2Xc6rJAoGAIo70MZO/s24NWVOEadDoJcdqXU52wYHJAgE96jK23yyoROHJ1UesN8c2HaHHfe385tOeSDhIFUAkJAYtYQwdkM7qjdc+A2n5aiq3z/76+WzPf3gNStKgoo5Zy+HsWxONwK9qzPmb8IOmaHqUaJVkoPLtWq64jwITaZi/mQle7QM=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy7lRIACETnhhY0ZefzXTKtZvT/G43Uy3rC/T3NJaPtkXZx/ZVxXGV6bBHpZHC0s+K1Aii5sR3r6aNhTFOrS78FsqbCzFynh3a4SUCwXfoMph0KesacFLve3oVFjAPMlyPnT5oWkfHxvtcr5JgMM2WxnUqKtw6zDkYb4uRs3dmqdL4xc+6JbI3jFGdNXhnpjJCaG0mlctnoHcjJia9WR7FDU+dcv9bNtSB9mTtm9nQ8MWbR1WstIsLb86zx5ObSr4udluIe7u5xr1ewmOxrrtZjk8VTM01SCbQb8d+o5rKgEKJFE0S6TQC2ejl1/2B0nqgvGK1loT/aAkkxnveP27EQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url =  "http://n3q9781234.wicp.vip/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 订单超时时间
    private  String timeout_express = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout_express +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
